﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;

namespace CharGen
{
    public class Character
    {
        public Character()
        {
            Name = "";
            Age = 1;
            Sex = true;
            Experience = 0;
            Level = 1;
            Class = "";
            PlayerName = "";
            PointsSpent = 0;


            ResetStats();
        }

        #region Properties
        //General Character Properties

        // props
        public string Name { get; set; }
        public int Age { get; set; }
        public bool Sex { get; set; } //true : M false: F
        public int Experience { get; set; }
        public int Level { get; set; }
        public string Class { get; set; }
        public string PlayerName { get; set; }
        public int PointsSpent { get; set; }
        public string Portrait { get; set; }

        //Statistics
        public int Strength { get; set; }
        public int Dexterity { get; set; }
        public int Intelligence { get; set; }
        public int Charisma { get; set; }

        //Sub-Statistics
        //Strength
        public int Power { get; set; }
        public int Vitality { get; set; }
        public int Endurance { get; set; }
        public int Courage { get; set; }

        //Dexterity
        public int Acrobatics { get; set; }
        public int Precision { get; set; }
        public int Deception { get; set; }
        public int Reflex { get; set; }

        //Intelligence
        public int Mysticism { get; set; }
        public int Education { get; set; }
        public int Ingenuity { get; set; }
        public int Will { get; set; }

        //Charisma
        public int Appearance { get; set; }
        public int Speechcraft { get; set; }
        public int Empathy { get; set; }
        public int Faith { get; set; }
        #endregion Properties

        #region Methods

        private string Serialize()
        {
            try
            {
                var xmlserializer = new XmlSerializer(typeof(Character));
                var stringWriter = new StringWriter();
                using (var writer = XmlWriter.Create(stringWriter))
                {
                    xmlserializer.Serialize(writer, this);
                    return stringWriter.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred", ex);
            }
        }

        private static Character Deserialize(string data)
        {
            try
            {
                var serializer = new XmlSerializer(typeof(Character));
                var reader = new StreamReader(data);
                object deserialized = serializer.Deserialize(reader.BaseStream);
                return (Character)deserialized;
            }
            catch (Exception ex)
            {
                throw new Exception("An error occurred", ex);
            }
        }

        private void ResetStats()
        {
            Strength = 0;
            Power = 0;
            Vitality = 0;
            Endurance = 0;
            Courage = 0;

            Dexterity = 0;
            Precision = 0;
            Deception = 0;
            Reflex = 0;
            Acrobatics = 0;

            Charisma = 0;
            Appearance = 0;
            Speechcraft = 0;
            Faith = 0;
            Empathy = 0;

            Intelligence = 0;
            Mysticism = 0;
            Education = 0;
            Will = 0;
            Ingenuity = 0;
        }

        public bool SaveToXml(string path)
        {
            var xmlData = Serialize();
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xmlData);
            try
            {
                xmlDocument.Save(path);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static Character LoadFromXml(string path)
        {
            return Deserialize(path);
        }

        #endregion Methods
    }
}
